package com.brillilab.starter.controller.aio.method;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.vo.method.MethodAuditInfoVo;
import com.brillilab.domain.vo.method.MethodAuditListVo;
import com.brillilab.domain.vo.method.MethodAuditStateVo;
import com.brillilab.domain.vo.method.NewMethod;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.message.IEventAuditService;
import com.brillilab.service.core.method.IMethodRecommendAuditService;
import com.brillilab.service.logic.method.MethodRecommendAuditLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.service.module.message.template.enums.MessageTypeEnum;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.brillilab.starter.handler.RequestPower;

/**
 * 成员方法对比
 * @author lj
 *
 */
@RestController
@RequestMapping("/api/method/audit")
public class MethodAuditController {

	@Autowired
	private UsersLogic usersLogic;
	@Autowired
	private MethodRecommendAuditLogic methodRecommendAuditLogic;
	@Autowired
	private IMethodRecommendAuditService methodRecommendAuditService;
	
	/**
	 * 获取审核列表
	 * @param token
	 * @return
	 */
	@RequestPower(LabMemberPowerEnum.method)
	@GetMapping("/list")
	public ResponseVo getAuditList(Integer state, @RequestParam(defaultValue = "1") Integer  pageNum,
			@RequestParam(defaultValue = "10")Integer pageSize,@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		PageVo<MethodAuditListVo> auditList = methodRecommendAuditService.getAuditList(userVo.getLabId(), null, state, pageNum, pageSize);
		return ResponseVo.success(auditList);
	}
	
	/**
	 * 获取审核状态数量
	 * @param token
	 * @return
	 */
	@RequestPower(LabMemberPowerEnum.method)
	@GetMapping("/stateCount")
	public ResponseVo getStateCount(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		Map<String, Integer> stateCount = methodRecommendAuditService.getStateCount(userVo.getLabId());
		return ResponseVo.success(stateCount);
	}

	/**
	 * 取消推荐/重新推荐
	 * @param id 推荐方法审核ID
	 * @param operation 操作：0-取消推荐，1-重新推荐
	 * @return
	 */
	@RequestPower(LabMemberPowerEnum.method)
	@GetMapping("/operation")
	public ResponseVo operation(Long id, Integer operation) {
		Assert.isTrue(id!=null && operation!=null, "参数错误");
		methodRecommendAuditService.operation(id, operation);
		return ResponseVo.success();
	}
	
	/**
	 * 审核详情
	 * @param id 推荐方法审核ID
	 * @return
	 */
	@GetMapping("/info")
	public ResponseVo auditInfo(Long id) {
		Assert.isTrue(id!=null, "参数错误");
		MethodAuditInfoVo infoVo = methodRecommendAuditService.auditInfo(id);
		Assert.notNull(infoVo, "数据不存在");
		return ResponseVo.success(infoVo);
	}
	
	/**
	 * 审核按钮时查询审核状态
	 * @param token
	 * @return
	 */
	@RequestPower(LabMemberPowerEnum.method)
	@GetMapping("/state")
	public ResponseVo auditState(Long id) {
		Assert.isTrue(id!=null, "参数错误");
		MethodAuditStateVo stateVo = methodRecommendAuditService.auditState(id);
		return ResponseVo.success(stateVo);
	}
	
	/**
	 * 审核不通过
	 * @param id 推荐方法审核ID
	 * @param auditOpinion 审核意见
	 * @return
	 */
	@AspectLog
	@RequestPower(LabMemberPowerEnum.method)
	@GetMapping("/notAdopt")
	public ResponseVo notAdopt(Long id, String auditOpinion,@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.isTrue(id!=null, "参数错误");
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		MethodAuditStateVo adoptMessage = methodRecommendAuditLogic.adoptMessage(id);
		if(adoptMessage.getState()==1) {
			methodRecommendAuditLogic.notAdopt(userVo.getLabId(), userVo.getLabMemberId(), id, auditOpinion);
		}
		return ResponseVo.success(adoptMessage);
	}
	/**
	 * 审核通过
	 * @param id 推荐方法审核ID
	 * @return
	 */
	@AspectLog
	@RequestPower(LabMemberPowerEnum.method)
	@GetMapping("/adopt")
	public ResponseVo auditAdopt(Long id,@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		MethodAuditStateVo adoptMessage = methodRecommendAuditLogic.adoptMessage(id);
		if(adoptMessage.getState()==1) {
			methodRecommendAuditLogic.auditAdopt(userVo, id,false);
		}
		return ResponseVo.success(adoptMessage);
	}

	/**
	 * 管理员推荐方法直接通过
	 * @param jsonData
	 * @param token
	 * @return
	 */
	@AspectLog
	@RequestPower(LabMemberPowerEnum.method)
	@PostMapping("/adminRecommend")
	public ResponseVo adminRecommend(@RequestBody JSONObject jsonData, @RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		JSONArray jsonArray = jsonData.getJSONArray("methodIds");
		Assert.isTrue(jsonArray!= null, "参数缺失");
		List<Long> methodIds = Arrays.asList(jsonArray.toJavaObject(Long[].class));
		methodRecommendAuditLogic.adminRecommend(userVo, methodIds);
		return ResponseVo.success();
	}

	/**
	 * 管理员编辑方法后推荐直接通过
	 * @param newMethod
	 * @param token
	 * @return
	 */
	@AspectLog
	@RequestPower(LabMemberPowerEnum.method)
	@PostMapping("/adminEditRecommend")
	public ResponseVo adminEditRecommend(@RequestBody NewMethod newMethod, @RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		MethodAuditStateVo adoptMessage = methodRecommendAuditLogic.adoptMessage(newMethod.getAuditId());
		if(adoptMessage.getState()==1) {
			methodRecommendAuditLogic.adminEditRecommend(userVo, newMethod);
		}
		return ResponseVo.success(adoptMessage);
	}
	
	
	/**
	* @Description: 审核判断  获取比较方法列表
	* @Params: [otherMethodId, token]
	* @return com.brillilab.common.entity.ResponseVo
	* @author wuzhiyu
	* @date 2019/3/6 13:16 
	*/
	@GetMapping("/auditSameMethodList")
	public ResponseVo auditExistSameMethod(Long methodId,@RequestHeader(value = StarterConstant.TOKEN) String token){
		Assert.notNull(methodId,"参数缺失");
		UserInfoVo user = usersLogic.getUserByToken(token);
		return ResponseVo.success(methodRecommendAuditLogic.sameMethodList(methodId,user));
	}
	
}
